<template>
	<view style="padding: 20rpx;">
		<u-notify ref="pwd_worn" />
		<u-loading-page :loading="loading" loading-text="正在更新信息"></u-loading-page>
		<!-- 设置密码 -->
		<u-popup :show="show_password_setting" mode="center" :round="10" customStyle="padding: 20px">
			<u-form :model="change_pwd">
				<u-form-item label="输入密码"><u--input v-model="change_pwd.pwd" /></u-form-item>
				<u-form-item label="重复密码"><u--input v-model="change_pwd.pwd_cert" /></u-form-item>
				<u-row gutter="10" justify="around">
					<u-col span="4">
					<u-button @click="cancel_pwd()" color="rgb(0, 122, 153)" plain="true" size="large">取消</u-button>
					</u-col>
					<u-col span="4">
					<u-button @click="post_pwd()" color="rgb(0, 122, 153)" plain="true" size="large">提交</u-button>
					</u-col>
				</u-row>
			</u-form>
		</u-popup>
		<u-form :model="user_data">
			<u-form-item label="用户名" borderBottom="true" label-width="80px">
				<u--input v-model="user_data.username" border="none"></u--input>
			</u-form-item>
			<u-form-item label="昵称" label-width="80px" borderBottom="true">
				<u--input v-model="user_data.nickname" border="none"></u--input>
			</u-form-item>
		</u-form>
		<u-upload @afterRead="avatar_handle" :maxCount="4" :fileList="avatar_list" :previewFullImage="true"></u-upload>
		<u-button @click="post()" color="rgb(0, 122, 153)" plain="true" size="large">提交</u-button>
		<u-button @click="() => {this.show_password_setting = true}" color="rgb(0, 122, 153)" plain="true" size="large">设置密码</u-button>
	</view>
</template>

<script lang="js">
	// import { data } from '../../../uni_modules/uview-ui/libs/mixin/mixin';
	import sha256 from '@/static/sha256.js'
	import utils from '@/static/uploadFile.js'
import avatar from '../../../uni_modules/uview-ui/libs/config/props/avatar';
import upload from '../../../uni_modules/uview-ui/libs/config/props/upload';
	export default {
		data() {
			return {
				show_avatar_picker: false,
				show_password_setting: false,
				user_data: {
					username: getApp().globalData.local_user.username,
					nickname: getApp().globalData.local_user.nickname,
					avatar_url: null,
				},
				change_pwd: {
					pwd: "",
					pwd_cert: "",
				},
				avatar_list: [],
				loading: false
			};
		},
		methods: {
			cancel_pwd() {
				this.show_password_setting = false
			},
			post_pwd() {
				if(this.change_pwd.pwd === this.change_pwd.pwd_cert && this.change_pwd.pwd.length >=8)
				{
					let new_password = sha256(this.change_pwd.pwd)
					console.log(new_password)
					uni.request({
						url: getApp().globalData.base_url + "/set/user_pwd",
						header: {token: getApp().globalData.access_token},
						method: "POST",
						data: new_password
					})
					this.show_password_setting = false
				} else {
					this.$refs.pwd_worn.show({
						type: "warning",
						color: "white",
						bgColor: "red",
						message: "两次密码不一致或长度过短",
						
					})
				}
			},
			post() {
				uni.request({
					method: "POST",
					header: {token: getApp().globalData.access_token},
					url:getApp().globalData.base_url + "/set/user_info",
					data: this.user_data,
				})
				.then((data) => {
					getApp().globalData.refreshLoginStatus()
					this.loading = true
					setTimeout(()=>{
						uni.switchTab({
							url: "/pages/me/me"
						})
					},500)
				})
				
			},
			avatar_handle(data) {
				this.loading = true
				this.avatar_list.push({ url: data.file.url })
				uni.request({
					url:getApp().globalData.base_url + "/get/avatar_update",
					header: {token : getApp().globalData.access_token}
				})
				.then(upload_url => {
					utils.uploadImageToTencentCloud(data.file.url, upload_url.data, null, null)
					this.user_data.avatar_url = "a" 
					this.loading = false
				})
				.catch(() => {
					uni.showModal({
						title:"上传失败",
					})
					this.loading = false
				})
			},
		}
	}
</script>

<style lang="scss">

</style>
